﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace WebNetCorePhoneErp.Model.Tool
{
    //将AES加密成16进制字符串
    public class AesHelpter
    {
        /// <summary>
        /// 默认密钥向量IV，限制16位长度
        /// </summary>
        private const string _iv = "1462#$@/*opAB8.M";

        /// <summary>
        /// 默认密钥向量KEY，限制16位长度
        /// </summary>
        private const string _key = "asldf#$%HJ@/g*jk";

        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="input"></param>
        /// <returns>base64字符串</returns>
        public static string AESEncrypt(string input)
        {
            var encryptKey = Encoding.UTF8.GetBytes(_key);
            var iv = Encoding.UTF8.GetBytes(_iv);
            using (var aesAlg = Aes.Create())
            {
                using (var encryptor = aesAlg.CreateEncryptor(encryptKey, iv))
                {
                    using (var msEncrypt = new MemoryStream())
                    {
                        using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                        using (var swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(input);
                        }
                        var decryptedContent = msEncrypt.ToArray();
                        return Convert.ToBase64String(decryptedContent);
                    }
                }
            }
        }

        /// <summary>
        /// AES加密，将字符串加密为16进制字符串
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <returns>加密后16进制字符串</returns>
        public static string AESEncryptToHex(string input)
        {
            var encryptKey = Encoding.UTF8.GetBytes(_key);
            var iv = Encoding.UTF8.GetBytes(_iv);
            using (var aesAlg = Aes.Create())
            {
                using (var encryptor = aesAlg.CreateEncryptor(encryptKey, iv))
                {
                    using (var msEncrypt = new MemoryStream())
                    {
                        using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                        using (var swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(input);
                        }
                        var decryptedContent = msEncrypt.ToArray();
                        // return Convert.ToBase64String(decryptedContent);

                        //转换为16进制字符串
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < decryptedContent.LongLength; i++)
                        {
                            sb.Append(decryptedContent[i].ToString("X2"));
                        }
                        return sb.ToString();
                    }
                }
            }
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string AESDecrypt(string input)
        {
            var fullCipher = Convert.FromBase64String(input);
            var decryptKey = Encoding.UTF8.GetBytes(_key);
            var iv = Encoding.UTF8.GetBytes(_iv);
            using (var aesAlg = Aes.Create())
            {
                using (var decryptor = aesAlg.CreateDecryptor(decryptKey, iv))
                {
                    string result;
                    using (var msDecrypt = new MemoryStream(fullCipher))
                    {
                        using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {
                            using (var srDecrypt = new StreamReader(csDecrypt))
                            {
                                result = srDecrypt.ReadToEnd();
                            }
                        }
                    }
                    return result;
                }
            }
        }

        /// <summary>
        /// 16进制字符串，AES解密
        /// </summary>
        /// <param name="input">16进制字符串</param>
        /// <returns></returns>
        public static string AESDecryptByHex(string input)
        {
            byte[] fullCipher = new byte[input.Length / 2];
            for (int i = 0; i < fullCipher.Length; i++)
            {
                fullCipher[i] = Convert.ToByte(input.Substring(i * 2, 2), 16);
            }
            var decryptKey = Encoding.UTF8.GetBytes(_key);
            var iv = Encoding.UTF8.GetBytes(_iv);
            using (var aesAlg = Aes.Create())
            {
                using (var decryptor = aesAlg.CreateDecryptor(decryptKey, iv))
                {
                    string result;
                    using (var msDecrypt = new MemoryStream(fullCipher))
                    {
                        using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {
                            using (var srDecrypt = new StreamReader(csDecrypt))
                            {
                                result = srDecrypt.ReadToEnd();
                            }
                        }
                    }
                    return result;
                }
            }
        }

    }
}